<template>
<view>


<view v-if="webview_url != ''">
	<web-view :src="webview_url" @message="webviewMessage" @load="webviewLoad"></web-view>
</view>


<view v-if>
  <tabbar :tabbar="tabbar"></tabbar>
</view>
</view>
</template>

<script>
var app = getApp();
var common = require('../../utils/common.js');
var _this;

export default {
  data() {
    return {
      webview_url: '',
      userInfo: '',
      title: '',
      shareResult: {},
      showShare: true,
      urlList: [],
      needUpdate: false
    };
  },

  onShow() {
    if (_this.needUpdate) {
      _this.needUpdate = false;
	  
      _this.setData({
        webview_url: ''
      }, function () {
        var url = _this.urlList[_this.urlList.length - 2];

        _this.urlList.splice(_this.urlList.length - 2, 2);
		
        _this.setData({
          webview_url: url
        });
      });
    }
  },

  onLoad: function (options) {
    _this = this; // wx.hideTabBar({})

    common.editTabbar({
      hidden: true
    });
    var webview_url = app.globalData.tabWeb.url;
    var webview_title = app.globalData.tabWeb.title;

    if (webview_title && webview_title != '') {
      common.getTxtKey(webview_title).then(key => {
        wx.setNavigationBarTitle({
          title: key
        });
      }, () => {});

      _this.setData({
        title: webview_title
      });
    } else {
      common.getTxtKey('打开页面').then(key => {
        wx.setNavigationBarTitle({
          title: key
        });
      }, () => {});
    }

    webview_url = webview_url.replace('http://', 'https://');

    if (webview_url.indexOf('https://') !== 0 && webview_url.indexOf('https%3A%2F%2F') === 0) {
      webview_url = decodeURIComponent(webview_url);
    }

    console.log(webview_url);
    var ticket = wx.getStorageSync('ticket');

    if (ticket) {
      webview_url = common.changeURLArg(webview_url, 'ticket', ticket);
      webview_url = common.changeURLArg(webview_url, 'device_id', 'wxapp');
      this.setData({
        userInfo: app.globalData.userInfo,
        showWeb: true
      });
      app.homeIsRedirect();

      if (app.globalData.scene == 1044 || app.globalData.scene == 1007) {
        wx.showToast({
          title: options.shareTickets,
          icon: 'success',
          duration: 5000
        });
        var obj = {
          'spread_openid': options.openId,
          'now_user_openid': userInfo.wxapp_openid,
          'code': wx.getStorageSync('code')
        };
        common.post('Home&a=wxapp_openid', obj, "scene", _this);
      }
    }

    _this.setData({
      webview_url: webview_url
    });

    common.setMainColor(_this, function (config) {
      _this.webviewLoad(webview_url);
    });
  },
  onShareAppMessage: function (options) {
    if (_this.shareResult.share_img && _this.shareResult.share_title) {
      let pageLink = _this.shareResult.share_url
      pageLink = common.addShareParam(pageLink)
	  return {
        title: _this.shareResult.share_title,
        imageUrl: _this.shareResult.share_img,
        path: pageLink
      };
    } else if (_this.shareData) {
      let pageLink = '/pages/plat_menu/index?redirect=webview&webview_url=' + encodeURIComponent(_this.shareData.sendFriendLink) + "&webview_title=" + encodeURIComponent(_this.shareData.tTitle)
      pageLink = common.addShareParam(pageLink)
	  return {
        title: _this.shareData.tTitle,
        path: pageLink
      };
    } else {
		let pageLink = _this.shareResult.share_url
		pageLink = common.addShareParam(pageLink)
      return {
        title: _this.title,
        path: pageLink
      };
    }
  },
  components: {},
  props: {},
  methods: {
    webviewLoad(e) {
      console.log('webview参数', e);

      if (typeof e == 'object') {
        var url = e.detail.src;

        _this.urlList.push(url);

        console.log('webview参数999999999999999988888888888', _this.urlList);
      } else {
        var url = e;
      }

      if (typeof e == 'object' && url == _this.webview_url) {
        return;
      } //判断页面跳转
      //首页


      if (url.indexOf('c=Home&a=index') != -1 || common.strEndWith(url, '/wap.php')) {
        if (common.checkIsFooterPage("/pages/plat_menu/index")) {
          wx.switchTab({
            url: "/pages/plat_menu/index"
          });
        } else {
          _this.needUpdate = true;
          wx.navigateTo({
            url: '/pages/nofooter/index'
          });
        }

        return;
      } //我的


      if (url.indexOf('c=My&a=index') != -1) {
        if (common.checkIsFooterPage("/pages/plat_menu/my")) {
          wx.switchTab({
            url: "/pages/plat_menu/my"
          });
        } else {
          _this.needUpdate = true;
          wx.navigateTo({
            url: '/pages/nofooter/my_index'
          });
        }

        return;
      } //支付


      if (url.indexOf('c=Pay&a=check') != -1) {
        var order_id = common.getUrlParam(url, 'order_id');
        var type = common.getUrlParam(url, 'type');
        _this.needUpdate = true;
        wx.navigateTo({
          url: "/pages/pay/index?order_id=" + order_id + "&type=" + type
        });
        return;
      } //快店首页


      if (common.strEndWith(url, 'c=Shop&a=index') || common.strEndWith(url, 'c=Shop&a=classic_index')) {
        if (common.checkIsFooterPage("/pages/shop_index/index")) {
          wx.switchTab({
            url: "/pages/shop_index/index"
          });
        } else {
          _this.needUpdate = true;
          wx.navigateTo({
            url: '/pages/nofooter/shop/shop_index'
          });
        }

        return;
      } //请求分享参数


      console.log('config', _this.config);
      var postParam = {
        webview_url: url,
        noTip: true
      };

      if (_this.config.city && _this.config.city.top_city && _this.config.city.top_city.area_id) {
        postParam['now_city'] = _this.config.city.top_city.area_id;
      }

      if (_this.config.city && _this.config.city.top_city && _this.config.city.area_id != _this.config.city.top_city.area_id) {
        postParam['now_area'] = _this.config.city.area_id;
      }

      common.post('Wxapp&a=webview_share', postParam, function (result) {
        _this.setData({
          shareResult: result
        });

        wx.setNavigationBarColor({
          frontColor: result.frontColor,
          backgroundColor: result.backgroundColor
        });

        if (result.shareMenu == true) {
          _this.showShare = true;
          wx.showShareMenu({
            withShareTicket: true
          });
        } else {
          _this.showShare = false;
          wx.hideShareMenu();
        }
      });
      _this.isFirstLoad = false;

      if (_this.title) {
        wx.setNavigationBarTitle({
          title: common.changeTxt(_this.title)
        });
      }
    },

    webviewMessage: function (e) {
      var _this = this;

      console.log('e.detail.data', e.detail.data);
      var messageData = e.detail.data[e.detail.data.length - 1];

      if (messageData.page && messageData.page == 'changecity') {
        delete messageData.page;

        if (_this.webview_url.indexOf('from') != -1) {
          wx.setStorageSync('kuaidian_city', messageData);
        } else {
          wx.setStorageSync('now_city_info', messageData);
        }

        return false;
      }

      for (var i = e.detail.data.length - 1; i >= 0; i--) {
        var messageData = e.detail.data[i];
        console.log('messageData:' + i, messageData);

        if (messageData.msgType && messageData.msgType == 'share') {
          console.log('shareData', messageData);
          this.setData({
            shareData: messageData
          });
        }

        return false;
      }
    },
    scene: function () {},
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
/* pages/webview/tab_webview.css */
</style>